﻿// //NOTE: We're no longer using this, but some of the code in here can be reused when we implement the open from cloud and isolated storage.


//using System.Collections.ObjectModel;
//using System.ComponentModel;
//using System.Windows;
//using MyFloridaHouse.libSvCommonUtilities;
//using MyFloridaHouse.ServiceConn.DistrBldrSvcRef;
//using MyFloridaHouse.libSvOpenDistricts.DataService;
//using MyFloridaHouse.libSvOpenDistricts.enumerations;
//using MyFloridaHouse.libSvUtilities.Logging;
//using MyFloridaHouse.libSvOpenDistricts.StorageIsolated;
//using System.IO.IsolatedStorage;
//using System;
//using System.Windows.Controls;

//namespace MyFloridaHouse.libSvOpenDistricts.Windows
//{
//    public partial class FileOpen : INotifyPropertyChanged
//    {

//        #region INotifyPropertyChanged Members
//        private PropertyChangedEventHandler propertyChanged;
//        public event PropertyChangedEventHandler PropertyChanged
//        {
//            add { propertyChanged += value; }
//            remove { propertyChanged -= value; }
//        }

//        protected void RaisePropertyChanged(string propertyName)
//        {
//            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
//        }

//        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
//        {
//            if (propertyChanged != null) { propertyChanged(this, e); }
//        }
//        #endregion

//        #region properties

//        private bool _isFileOpenActive = false;
//        public bool isFileOpenActive
//        {
//            get { return _isFileOpenActive; }
//            set
//            {
//                if (_isFileOpenActive != value)
//                {
//                    _isFileOpenActive = value;
//                    RaisePropertyChanged("isFileOpenActive");
//                }
//            }
//        }

//        private bool _isFileLoading = false;
//        public bool isFileLoading
//        {
//            get { return _isFileLoading; }
//            set
//            {
//                if (_isFileLoading != value)
//                {
//                    _isFileLoading = value;
//                    RaisePropertyChanged("isFileLoading");
//                }
//            }
//        }

//        private ObservableCollection<FileInfoDTO> _fileOpenFileNameList = new ObservableCollection<FileInfoDTO>();
//        public ObservableCollection<FileInfoDTO> fileOpenFileNameList
//        {
//            get { return _fileOpenFileNameList; }
//            set
//            {
//                _fileOpenFileNameList = value;
//                RaisePropertyChanged("fileOpenFileNameList");
//            }
//        }

//        private int _fileOpenFileNameListSelectedIndex = -1;
//        public int fileOpenFileNameListSelectedIndex
//        {
//            get { return _fileOpenFileNameListSelectedIndex; }
//            set
//            {
//                if (_fileOpenFileNameListSelectedIndex != value)
//                {
//                    _fileOpenFileNameListSelectedIndex = value;
//                    if ((_fileOpenFileNameListSelectedIndex < 0) | (_fileOpenFileNameListSelectedIndex > _fileOpenFileNameList.Count - 1))
//                    {
//                        fileOpenFileName = "";
//                    }
//                    else
//                    {
//                        fileOpenFileName = fileOpenFileNameList[fileOpenFileNameListSelectedIndex].fileName;
//                    }
//                    RaisePropertyChanged("fileOpenFileNameListSelectedIndex");
//                }
//            }
//        }

//        private string _fileOpenFileName = "";
//        public string fileOpenFileName
//        {
//            get { return _fileOpenFileName; }
//            set
//            {
//                if (_fileOpenFileName != value)
//                {
//                    _fileOpenFileName = value;
//                    RaisePropertyChanged("fileOpenFileName");
//                }
//            }
//        }


//        private enumFileOpenFileSource _fileOpenFileSource = enumFileOpenFileSource.CLOUD;
//        public enumFileOpenFileSource fileOpenFileSource
//        {
//            get { return _fileOpenFileSource; }
//            set
//            {
//                if (_fileOpenFileSource != value)
//                {
//                    _fileOpenFileSource = value;
//                    GetFileInfoFromSource();
//                    RaisePropertyChanged("fileOpenFileSource");
//                }
//            }
//        }
//        #endregion

//        #region Methods

//        ServiceDAL serviceDAL;
//        private xFileOpen fileOpenWindow = new xFileOpen();

//        public FileOpen(ServiceDAL serviceDAL, StackPanel childWindowStackPanel)
//        {
//            this.serviceDAL = serviceDAL;
//            IntializeCommands();


//            // add the fileOpen window to the application window 
//            fileOpenWindow.HorizontalAlignment = HorizontalAlignment.Center;
//            childWindowStackPanel.Children.Add(fileOpenWindow);
//            fileOpenWindow.DataContext = this;
//        }

//        public void Begin()
//        {
//            isFileLoading = true;

//            // if we last pointed to local storage and it's system window, repoint to cloud so we can use our file open window
//            if (fileOpenFileSource == enumFileOpenFileSource.LOCAL)
//            {
//                fileOpenFileSource = enumFileOpenFileSource.CLOUD;
//                // we'll open the window when we get the available file list from the cloud
//            }
//            else 
//            {
//                GetFileInfoFromSource();
//            }

//        }

//        public void OpenWindow()
//        {
//            isFileOpenActive = true;
//        }

//        public void CloseWindow()
//        {
//            isFileOpenActive = false;
//        }


//        #endregion

//        public bool CanOpen()
//        {
//            return (fileOpenFileNameListSelectedIndex >= 0);
//        }

//        public FileInfoDTO GetSelectedFile()
//        {
//            return fileOpenFileNameList[fileOpenFileNameListSelectedIndex];
//        }

//        public void GetFileInfoFromSource()
//        {
//            if (fileOpenFileSource == enumFileOpenFileSource.CLOUD)
//            {
//                serviceDAL.GetFileInfo(new System.EventHandler<GetFileInfoCompletedEventArgs>(GetFileInfoCompleted));
//                // open dialog window later, after response from cloud
//            }
//            else if (fileOpenFileSource == enumFileOpenFileSource.LOCAL)
//            {
//                // open a local storage dialog, which actually allows us to pick a file and start the open
//                OpenFileFromLocalStorage();
//                _fileOpenFileSource = enumFileOpenFileSource.CLOUD;
//                CloseWindow();
//            }
//            else if (fileOpenFileSource == enumFileOpenFileSource.ISOLATED)
//            {
//                // for isolated, we can look right away, and display a list of files to be opened
//                fileOpenFileNameList.Clear();
//                IsolatedStorageManager.GetFileInfo(Constants.DistrictPlanDirectoryName, "*" + Constants.DistrictPlanFileNameExtension, fileOpenFileNameList);
//                fileOpenFileNameListSelectedIndex = -1;
//                OpenWindow();
//            }
//            else
//            {
//                // todo: log invalid file source
//            }
//        }

//        private void GetFileInfoCompleted(object sender, GetFileInfoCompletedEventArgs args)
//        {
//            // error in WCF service call
//            if (Log.HandleException(args.Error)) return;

//            // unknown error in the service method
//            if (args.Result == null)
//            {
//                MessageBox.Show("Error getting File Info from service " + args.Error.Message.ToString());
//            }
//            else
//            {
//                // caught error in the service method
//                if (Log.HandleServerError("GetFileInfoCompleted", args.Result.errorMessage)) return;

//                // Everything OK!
//                LoadFileInfo(args.Result);
//            }
//        }


//        public void LoadFileInfo(FileInfoSTO args)
//        {
//            // : first clear out old values from fileOpenFileNameList
//            fileOpenFileNameList.Clear();
//            foreach (var fileInfo in args.files)
//            {
//                fileOpenFileNameList.Add(fileInfo);
//            }
//            fileOpenFileNameListSelectedIndex = -1;
//            OpenWindow();
//        }

//        public void GetFileFromSource()
//        {
//            if (fileOpenFileSource == enumFileOpenFileSource.CLOUD)
//            {
//                MessageBox.Show("Opening File - this will take a few minutes.  Please be patient");
//                serviceDAL.GetFile(GetSelectedFile(), new System.EventHandler<GetFileCompletedEventArgs>(GetCloudFileCompleted));
//            }
//            else if (fileOpenFileSource == enumFileOpenFileSource.ISOLATED)
//            {
//                OpenFileFromIsolatedStorage();
//            }
//            else if (fileOpenFileSource == enumFileOpenFileSource.LOCAL)
//            {
//                // local storage not accessed at this point
//            }
//            else
//            {
//                // todo: log invalid file source
//                Log.HandleInternalError("Invalid File Source detected, can't load file");
//            }
//            CloseWindow();
//        }


//        public void GetCloudFileCompleted(object sender, GetFileCompletedEventArgs args)
//        {

//            // error in WCF service call
//            if (Log.HandleException(args.Error)) return;

//            // unknown error in the service method
//            if (args.Result == null)
//            {
//                Log.HandleServerError("GetCloudFileCompleted", "Unknown Error getting File from Service ");
//            }
//            else
//            {
//                // caught error in the service method
//                if (Log.HandleServerError("GetCloudFileCompleted", args.Result.errorMessage)) return;

//                // Everything OK!
//                if (args == null)
//                {
//                    Log.HandleInternalError("GetCloudFileCompleted: Unable to load file, invalid file name selected");
//                    return;
//                }

//                try
//                {
//                    isFileLoading = true;
//                    FileOpenCompleteEventArgs fileOpenCompleteArgs = new FileOpenCompleteEventArgs();
//                    fileOpenCompleteArgs.fileInfo = new FileInfoDTO();
//                    fileOpenCompleteArgs.fileInfo.fileName = args.Result.fileInfo.fileName;
//                    fileOpenCompleteArgs.fileInfo.format = args.Result.fileInfo.format;
//                    fileOpenCompleteArgs.fileData = args.Result.fileData;
//                    CloseWindow();
//                    RaiseFileOpenComplete(this, fileOpenCompleteArgs);
//                }
//                catch (Exception ex)
//                {
//                    Log.HandleInternalError("GetCloudFileCompleted: Error opening cloud file, " + ex.Message.ToString());
//                }
//                finally
//                {
//                    isFileLoading = false;
//                }

//            }
//        }


//        public void OpenFileFromIsolatedStorage()
//        {
//            string fileDataString;

//            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForSite())
//            {
//                fileDataString = IsolatedStorageManager.LoadStringData(Constants.DistrictPlanDirectoryName, fileOpenFileName + Constants.DistrictPlanFileNameExtension);
//                isf.Dispose();
//            }
//            if (fileDataString == null)
//            {
//                Log.HandleInternalError("Unable to load file from isolated storage");
//            }
//            else
//            {
//                try
//                {
//                    isFileLoading = true;
//                    FileOpenCompleteEventArgs fileOpenCompleteArgs = new FileOpenCompleteEventArgs();
//                    fileOpenCompleteArgs.fileInfo = new FileInfoDTO();
//                    fileOpenCompleteArgs.fileInfo.fileName = fileOpenFileName;
//                    fileOpenCompleteArgs.fileInfo.format = Constants.PlanFileFormatXML;
//                    System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
//                    fileOpenCompleteArgs.fileData = encoding.GetBytes(fileDataString);
//                    CloseWindow();
//                    RaiseFileOpenComplete(this, fileOpenCompleteArgs);
//                }
//                catch (Exception ex)
//                {
//                    Log.HandleInternalError("OpenFileFromIsolatedStorage: Error opening file from isolated storage, " + ex.Message.ToString());
//                }
//                finally
//                {
//                    isFileLoading = false;
//                }
//            }
//        }


//        public void OpenFileFromLocalStorage()
//        {
//            try
//            {
//                isFileLoading = true;

//                //OpenDistrictPlan(fileOpenFileName, districtListFromLocalFile);
//                string fileDataString;
//                // Create an instance of the open file dialog box.
//                OpenFileDialog openFileDialog = new OpenFileDialog();

//                // Set filter options and filter index.
//                openFileDialog.Filter = "XML (.xml)|*.xml|Freds (.txx)|*.txx|All Files (*.*)|*.*";
//                openFileDialog.FilterIndex = 1;
//                openFileDialog.Multiselect = true;

//                // Call the ShowDialog method to show the dialog box.
//                bool? userClickedOK = openFileDialog.ShowDialog();

//                // Process input if the user clicked OK.
//                if (userClickedOK == true)
//                {
//                    fileOpenFileName =openFileDialog.File.Name;

//                    // Open the selected file to read.
//                    System.IO.Stream fileStream = openFileDialog.File.OpenRead();

//                    using (System.IO.StreamReader reader = new System.IO.StreamReader(fileStream))
//                    {
//                        // Read the first line from the file and write it the textbox.
//                        byte[] binaryData = null;
//                        fileDataString = reader.ReadToEnd();

//                        FileOpenCompleteEventArgs fileOpenCompleteArgs = new FileOpenCompleteEventArgs();
//                        fileOpenCompleteArgs.fileInfo = new FileInfoDTO();
//                        fileOpenCompleteArgs.fileInfo.fileName = fileOpenFileName;
//                        if (openFileDialog.File.Extension == ".xml")
//                        {
//                            fileOpenCompleteArgs.fileInfo.format = Constants.PlanFileFormatXML;
//                            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
//                            binaryData = encoding.GetBytes(fileDataString);
//                        }
//                        else if (openFileDialog.File.Extension == ".txx")
//                        {
//                            fileOpenCompleteArgs.fileInfo.format = Constants.PlanFileFormatFreds;
//                            binaryData = CompressionManager.Compress(fileDataString);
//                        }


//                        fileOpenCompleteArgs.fileData = binaryData;
//                        RaiseFileOpenComplete(this, fileOpenCompleteArgs);
//                    }
//                    fileStream.Close();
//                }
//                CloseWindow();

//            }
//            catch (Exception ex)
//            {
//                Log.HandleInternalError("OpenFileFromLocalStorage: Error opening file from local storage, " + ex.Message.ToString());
//            }
//            finally
//            {
//                isFileLoading = false;
//            }

//        }
//    }
//}
